#!perl -T

use strict;
use warnings;
use Test::More tests => 21;
use Data::Dumper;

BEGIN {
	use_ok( 'NaturalDateGuesser' );
}

my $parser = NaturalDateGuesser->new();

# this is for testing the underlying
# internals of the parser so that I have
# a reasonable chance of not breaking it

my $chunks = $parser->_getChunks("20 January 2006");
my $expected = [ 
	{ data => 20, number => 1 }, 
	{ data => "January", word => 1, month => 1, 
		value => "January" }, 
	{ data => 2006, number => 1, year => 1 } 
];

is_deeply( $chunks, $expected, "Chunk 20 January 2006" );

$chunks = $parser->_getChunks("Next Wednesday");
$expected = [ 
	{ data => "Next", word => 1, modifier => 1, 
		value => "Next" }, 
	{ data => "Wednesday", word => 1, day_of_week => 1, 
		value => "Wednesday" } 
];

is_deeply( $chunks, $expected, "Chunk Next Wednesday" );

$chunks = $parser->_getChunks("Next Thursday");
$expected = [ 
	{ data => "Next", word => 1, modifier => 1, 
		value => "Next" }, 
	{ data => "Thursday", word => 1, day_of_week => 1, 
		value => "Thursday" } 
];

is_deeply( $chunks, $expected, "Chunk Next Thursday" );

$chunks = $parser->_getChunks("Next thursday");
$expected = [ 
	{ data => "Next", word => 1, modifier => 1, 
		value => "Next" }, 
	{ data => "thursday", word => 1, day_of_week => 1, 
		value => "Thursday" } 
];

is_deeply( $chunks, $expected, "Chunk Next thursday" );

$chunks = $parser->_getChunks("20th January 2006");
$expected = [ 
	{ data => 20, number => 1 }, 
	{ data => "th", word => 1, decoration => 1, 
		value => "th" }, 
	{ data => "January", word => 1, month => 1, 
		value => "January" }, 
	{ data => 2006, number => 1, year => 1 } 
];

is_deeply( $chunks, $expected, "Chunk 20th January 2006" );

$chunks = $parser->_getChunks("In 3 Days");
$expected = [ 
	{ data => "In", word => 1, modifier => 1, 
		value => "In" }, 
	{ data => 3, number => 1 }, 
	{ data => "Days", word => 1, unit => 1, 
		value => "Days" }, 
];

is_deeply( $chunks, $expected, "Chunk In 3 Days" );

$chunks = $parser->_getChunks("In 1 Day");
$expected = [ 
	{ data => "In", word => 1, modifier => 1, 
		value => "In" }, 
	{ data => 1, number => 1 }, 
	{ data => "Day", word => 1, unit => 1, 
		value => "Days" }, 
];

is_deeply( $chunks, $expected, "Chunk In 1 Day" );

ok( testCompleteKeyWord($parser, "January"), "Keyword test" );
ok( testCompleteKeyWord($parser, "Today"), "Keyword test" );
ok( testCompleteKeyWord($parser, "Next"), "Keyword test" );
ok( testCompleteKeyWord($parser, "thursday"), "Keyword test" );

# th is an exception becuse it cannot be recognised
# lest we mess up Thursday
ok( !testCompleteKeyWord($parser, "th"), "Keyword test" );
ok( !testCompleteKeyWord($parser, "Jan"), "Keyword test" );
ok( !testCompleteKeyWord($parser, "Toda"), "Keyword test" );
ok( !testCompleteKeyWord($parser, "t"), "Keyword test" );
ok( !testCompleteKeyWord($parser, "smeg"), "Keyword test" );

ok( KeyWordProcessTest($parser, "MON", "day_of_week", 
						"Monday"), "Token processing");
ok( KeyWordProcessTest($parser, "Mon", "day_of_week", 
						"Monday"), "Token processing");
ok( KeyWordProcessTest($parser, "Today", "relative_day", 
						"Today"), "Token processing");

# regexp chars in the text should not be 
# processed as regexp chars

ok( !KeyWordProcessTest($parser, "To.ay", "relative_day", 
						"Today"), "Regexp test");

sub KeyWordProcessTest
{
	my ($parser, $string, $property, $value) = @_;
	my $chunk = { data => $string, word => 1 };

	$parser->_completeProcessing( $chunk );

	if($value)
	{
		if(!$chunk->{value} eq $value)
		{
			return 0;
		}
	}
	return $chunk->{$property};
}

sub testCompleteKeyWord
{
	my ($parser, $string) = @_;
	my $chunk = { data => $string };
	my $result = $parser->_isCompleteKeyWord($chunk);
	return $result;
}


